<template>
	<view>
		<div>
			<navitop> 
				<i class="iconfont icon-jiantou-copy" slot="left-slot" @click="outlot"></i>
				<span slot="center-slot">详情</span>
				<i class="iconfont icon-youcecaidan" slot="right-slot"  @click="tapPopup(data)"></i>
			</navitop>
		</div>
		
		<div class="detial" :style="'margin-top:' + nv_height">
			<scroll-view class="staff_b" scroll-y="true" touch-action=none; :style='window_height'>
				<div class="detial_text" v-for="(item,index) in dataSorce">
					<div v-if="item.onEdit == true">
						<div v-if=" item.code !== 'projectNo' && item.code !== 'crmName' && item.type == '文本' || item.type == '整数'">
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text><text style="color: #000000;">{{item.value}}</text>
						</div>
						<div v-else-if="item.code == 'crmName'">
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text><text style="color: #000000;">{{item.value}}</text>
						</div>
						<div v-else-if="item.type == '下拉'">
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text><text style="color: #000000;">{{item.value}}</text>
						</div>
						<div v-else-if="item.type == '搜索选择'">
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text><text style="color: #000000;">{{item.value}}</text>
						</div>
						<div v-else-if="item.type == '区块'" >
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text>
							<textarea v-model="item.value" style="border: 1px solid #cacaca;height: 100%;width: 70%;" disabled="true" >
							</textarea>
						</div>
						<div v-else-if="item.type == '时间' || item.type == '日期'">
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text><text>{{item.value}}</text>
						</div>
					</div>
				</div>
			</scroll-view>
		</div>
		<!-- 弹窗组件 -->
		<popus ref="alert">
			<alertcenter @row="row" :titles="titles" :rundata="rundata" :dataSorc="datas" ></alertcenter>
		</popus>
		
		<popus ref="alert_edit">
			<div :style="alertWidth">
				<businessdedit @closes="closes" :selid="selid"></businessdedit>
			</div>
		</popus>
		
		
		<chunLeiPopups v-model="value" :popData="data" @tapPopup="tapPopup" :x="345" :y="95" placement="top-end"></chunLeiPopups>
		
	</view>
</template>

<script>
	import navitop from '@/components/common/navi-top'
	import chunLeiPopups from "@/components/chunLei-popups/chunLei-popups"
	import alertcenter from '@/pages/common/common/alert/alertgx'
	import kxTime from "@/components/kx-datetime/kx-datetime"	
	import businessdedit from '@/pages/common/index/subpage/business/businessdedit'
	import popus from '@/uni_modules/uni-popup/components/uni-popup/uni-popup'
	import {business,list,ForSelect,listCompanyActivities,businesssave,disable} from '@/common/api/job/jobapi.js'
	import {user_name_list} from "@/common/api/job/alert.js"
	export default {
		components:{
			navitop,
			alertcenter,
			chunLeiPopups,
			popus,
			kxTime,
			businessdedit
		},
		props:{
			selid: {
				default: ''
			}
		},
		data(){
			return {
				window_height:'',
				alertWidth: '',
				dataSorce: [],
				rundata: [],
				datas: [],
				data: [{title:"编辑",code:2,icon: 'icon-bianji'},{title:"删除",code:3,icon: 'icon-shanchu'}],
				date: '', //需要展示的时间数据
				enddate: "2500", // 时间段的终止时间，不设置默认当前之间终止
				startdate: "2020" ,//时间段的开始时间，不设置默认计算机最初时间
				value:false,
				titles: '商机列表',
				
				heigs:'',
				heigsdiv: 0,
				status: [{name:'计划中',code:'activityState'},{name:'执行中',code:'activityState'},{name: '终止',code:'activityState'},{name: '完成',code:'activityState'}],
				time_code: '',	// 获取选中时间code
				actv_class: [],	// 特色	
				user_name: [],	// 负责人
				
				activeList: []	 ,// 获取活动列表
				userList: []	,// 获取用户列表
				
				public_detals: {},	//  获取原始数据
				
				nv_height: ''
			}
		},
		mounted() {
			 this.nv_height = uni.getSystemInfoSync().statusBarHeight + 60 + 'px';
			let _this = this;
			uni.getSystemInfo({
			    success: function (res) {
					_this.alertWidth = 'width:' + res.windowWidth + 'px;'
			    }
			});
			
			uni.onKeyboardHeightChange(res => {
				res.height = res.height + 50;
				this.heigsdiv = 'height:' + res.height  + 'px;' ;
				this.heigs = 'height:' + res.height + 'px;' ;
			})
		   const res = uni.getSystemInfoSync();
		   this.window_height = 'height:' + res.windowHeight + 'px;';
			
			
			
			
			// 获取详情
			this.business_datils();
			
			// 获取用户列表
			this.user_list();
			
			// 获取活动列表
			this.active_list();
			
			
		},
		methods:{
			
			// 关闭窗口
			outlot(){
				this.$emit('closes',2)
			},
			
			closes(){
				this.$refs.alert_edit.close();
			},
			
			// 获取时间code
			altime(val){
				this.time_code = val;
			},
			
			// 获取时间
			kxdatetime(e){
				for(let i in this.dataSorce){
					if(this.dataSorce[i].code == this.time_code){
						this.dataSorce[i].value = e.substr(0,10);
					}
				}
			},
			
			// 下拉处理 
			bus_dom(val){
				let rundata = [];
				this.datas = [];
				this.dataSorce.forEach(item =>{
					if(val == item.code){
						rundata = item.val.split(',');
						for(let i in rundata){
							this.datas.push({name:rundata[i],code: val})
							this.titles = item.name
						}
					}
				})
				this.$refs.alert.open('bottom')
			},
			
			// 搜索选择
			serach_choice(val){
				this.datas = [];
				if(val == 'activityId'){
					this.titles = '市场活动';
					for(let i in this.dataSorce){
						if(this.dataSorce[i].code == val){
							for(let i in this.activeList){
								this.datas.push({name:this.activeList[i].title,code: 'activityId'})
							}
						}
					}
				}
				this.$refs.alert.open('bottom')
				
			},
			
			
			// 客户名称单独处理
			name_dom(val){
				this.datas = this.userList;
				this.$refs.alert.open('bottom')
			},
			
			// 获取详情
			business_datils(){
				list({source: 'business'}).then((result) => {
					if(result.code == 200){
						business({id: this.selid}).then((rows) => {
							if(rows.code == 200){
								if(rows.data.defineFields !== '' && rows.data.defineFields !== null && rows.data.defineFields !== undefined){
									rows.data.defineFields = JSON.parse(rows.data.defineFields);
								}
								this.public_detals = rows.data;
								result.data.forEach(item => {
									if(item.dataType == 1){
										this.$set(item,'value',rows.data[item.code])
									}
									if(item.dataType == 2){
										if(rows.data.defineFields !== '' && rows.data.defineFields !== null && rows.data.defineFields !== undefined){
											this.$set(item, 'value', rows.data.defineFields[item.id])
										}
									}
								})
								this.dataSorce = result.data;
							}
							console.log(rows)
							console.log(this.dataSorce)
						})
					}
				})
				
			},
			
			// 获取用户列表
			user_list(){
				ForSelect({current: 1, size: 1000}).then((rows) => {
					if(rows.code == 200){
						for(let i in rows.data.records){
							this.userList.push({name: rows.data.records[i].title,id:rows.data.records[i].id,code: 'crmName'});
						}
					}
				})
			},
			
			// 获取活动列表
			active_list(){
				listCompanyActivities().then((rows) =>{
					if(rows.code == 200){
						this.activeList = rows.data;
					}
				})
			},
			
			// 获取弹窗结果 
			row(val) {
				for(let i in this.dataSorce){
					if(this.dataSorce[i].code == val.code ){
						this.dataSorce[i].value = val.name;
					}
				}
				this.$refs.alert.close()
			},
			
			// 保存
			 okclick(){
				 let pas = {};
				 let uname = {};
				 let define = '';
				for(let i in this.dataSorce){
					if(this.dataSorce[i].dataType == 1){
						uname[this.dataSorce[i].code] = this.dataSorce[i].value;
					}
					if( this.dataSorce[i].dataType == 2){
						define = {[this.dataSorce[i].id]: this.dataSorce[i].value}
					}
				}
				uname['defineFields'] = JSON.stringify(define);
				uname['id']	= this.selid;
				 businesssave(uname).then((rows)=> {
					 if(rows.code == 200){
						 uni.showToast({ 
						 	title: '保存成功'
						 })
						 setTimeout(()=>{
							 this.$emit('closes',1)
						 },1500)
					 }else{
						 uni.showToast({
						 	title: rows.message,
							icon: 'error'
						 })
					 }
				 })
			 },
			 
			 // 删除
			 tapPopup(val){
				 console.log(val)
				 this.value = true;
				 if(val.code == 3){
					 disable([this.selid]).then((rows)=>{
						 if(rows.code == 200){
							 uni.showToast({
								title:'删除成功'
							 })
							setTimeout(()=>{
								this.$emit('closes',1)
							},1500)
						 }
					 })
				 }
				 if(val.code == 2){
					 this.$refs.alert_edit.open('right')
				 }
				 
			 }
			
			
		}
	}
	
</script>

<style scoped lang="scss">
.detial {
	height: 100%;
	padding-top: 95rpx;
	width: 100%;
	background-color: #fff;
	.detial_text{
		div {
			width: 95%;
			display: flex;
			// justify-content: space-between;
			margin: auto;
			height: 90rpx;
			line-height: 89rpx;
			border-bottom: 1px solid #f0f0f0;
		}
		
	}
	.detial_text text{
		// width: 40%;
		text-align: left;
		padding-right: 10rpx;
		color: #808080;
	}
	.detial_text span{
		// padding-left: 30rpx;
	}
	.detial_text input{
		// height: 90rpx;
		width: 60%;
		// border: 1px solid #cacaca;
		border-radius: 4px;
	}
	
	input:hover {
			border: 1px solid #1890ff;
	}	
}
.stats {
	width: 200px;
	height: 100rpx;
	z-index: 1;
}
.title {
	margin-top: 1px;
	height: 90rpx;
	background-color: #fff;
	line-height: 90rpx;
	padding-left: 10px;
	border-bottom: 1px solid #f0f0f0;
	border-radius: 4px;
}
.second_list {
	height: 90rpx;
	margin-top: 1px;
	background-color: #fff;
	line-height: 90rpx;
	padding-left: 20px;
	border-radius: 4px;

}
</style>
